package SiTuQingYou.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;

import SiTuQingYou.dao.GuiderDao;
import SiTuQingYou.util.FileRename;
import SiTuQingYou.util.Pager;
import SiTuQingYou.vo.City;
import SiTuQingYou.vo.Guider;

/**
 * Servlet implementation class GuiderServlet
 */
@WebServlet("/GuiderServlet")
public class GuiderServlet extends BaseAction {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GuiderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void getSixGuider(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 ArrayList<Guider> guiders=GuiderDao.getgd().getSixGuider();
		 req.setAttribute("guiders", guiders);
		
	}
    //全部
    protected void getallguider(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	String name = req.getParameter("key");
		 ArrayList<Guider> gui=GuiderDao.getgd().allguider(name);
		 req.setAttribute("gui", gui);
		
	}
    //添加导游信息到数据库
    protected void addGuider(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获得当前项目在tomcat下的绝对路径
		String path = this.getServletContext().getRealPath("/");
		
		
		FileRename filerename = new FileRename();
		MultipartRequest mulreq = new MultipartRequest(request,path+"/GuiderHead",10*1024*1024,"utf-8",filerename);
		
		
		Guider guider = new Guider();
		
		guider.setGuidername(mulreq.getParameter("guidername"));//导游名字 String
		guider.setGuidersex(Boolean.parseBoolean(mulreq.getParameter("sex")));//导游性别 bit
		guider.setGuideridnumber(mulreq.getParameter("idnumbername"));//身份证号String
		guider.setGuiderphone(mulreq.getParameter("phonename"));//手机号String
		guider.setGuiderpirce(Float.parseFloat(mulreq.getParameter("pricename")));//导游费float
		guider.setGuiderinfo(mulreq.getParameter("informationname"));//导游信息String
		City city=new City();
		city.setId(Integer.parseInt(mulreq.getParameter("cityid")));
		guider.setCity(city);
	    guider.setGuiderphoto(mulreq.getFilesystemName("headname"));//导游头像 （二进制）
		
		
		GuiderDao.getgd().addOneGuider(guider);
		
		//response.sendRedirect("/SiTuQingYou/houtai/AddGuiderPage.jsp");
		response.sendRedirect("/SiTuQingYou/houtai/AddGuiderPage.jsp?t=1");
	}
    
    
    //查询出所有导游
    protected void lookForAllGuiders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	Pager pager = Pager.init(request);
    	
    	ArrayList<Guider>allGuider = GuiderDao.getgd().showAllGuiders(pager);
    	request.setAttribute("allGuider",allGuider);
    }
    
    //根据id查询导游信息
    protected void lookForInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String id = request.getParameter("info");
    	Guider guider = GuiderDao.getgd().loofForDetailedInfoById(Integer.parseInt(id));
    	request.setAttribute("guider",guider);
    }
    
    //修改导游信息
    protected void reviseGuider(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获得当前项目在tomcat下的绝对路径
		String path = this.getServletContext().getRealPath("/");
		FileRename filerename = new FileRename();
		MultipartRequest mulreq = new MultipartRequest(request,path+"/GuiderHead",10*1024*1024,"utf-8",filerename);
		
		Guider guider = new Guider();
    	
		guider.setId(Integer.parseInt(mulreq.getParameter("theid")));
		guider.setGuiderphone(mulreq.getParameter("phonename"));//手机号String
		guider.setGuiderpirce(Float.parseFloat(mulreq.getParameter("pricename")));//导游费float
		guider.setGuiderinfo(mulreq.getParameter("informationname"));//导游信息String
		if(mulreq.getFilesystemName("headname")!=null&&mulreq.getFilesystemName("headname")!="")
		{
			guider.setGuiderphoto(mulreq.getFilesystemName("headname"));//导游头像 （二进制）
		}
		City city = new City();
		city.setId(Integer.parseInt(mulreq.getParameter("cityid")));
	    guider.setCity(city);
		
		GuiderDao.getgd().toReviseGuiderInfo(guider);
		
		response.sendRedirect("/SiTuQingYou/houtai/newsType.jsp");
		 
    }
    
    
    protected void deleteOneGuider(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		 GuiderDao.getgd().deleteGuiderById(Integer.parseInt(request.getParameter("id")));
		 response.sendRedirect("/SiTuQingYou/houtai/newsType.jsp");
	}
    

	@Override
	protected void doDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 
		
	}
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	 

}
